﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Web;

namespace DNA.Mvc
{
    public static class Bootstrapper
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute("dna_opensearch", "{service}", new { controller = "Search", action = "Search" }, new { service = "search" });
            routes.MapRoute("dna_sitemaproute", "{service}", new { controller = "Syndication", action = "Sitemap" }, new { service = "sitemapindex" });
            routes.MapRoute("dna_rssFeeds", "{feeds}/{website}/{rss}", new { controller = "Syndication", action = "RssFeed", website = "home" }, new { feeds = "feeds", rss = "rss" });
            routes.MapRoute("dna_atomFeeds", "{feeds}/{website}/{atom}", new { controller = "Syndication", action = "AtomFeed", website = "home" }, new { feeds = "feeds", atom = "atom" });

            #region webfiles routes
            routes.MapRoute("dna_webfiles_copy", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "Copy" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("COPY") });

            routes.MapRoute("dna_webfiles_move", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "Move" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("MOVE") });

            routes.MapRoute("dna_webfiles_mkcol", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "CreatePath" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("MKCOL") });

            routes.MapRoute("dna_webfiles_get", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "GetFile" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("GET") });

            routes.MapRoute("dna_webfiles_put", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "Replace" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("PUT") });

            routes.MapRoute("dna_webfiles_post", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "Upload" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("POST") });

            routes.MapRoute("dna_webfiles_delete", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "Delete" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("DELETE") });

            routes.MapRoute("dna_webfiles_list", "{service}/{website}/{*path}",
    new { website = "home", controller = "WebFiles", action = "List" },
    new { service = "webshared", httpMethod = new HttpMethodConstraint("LIST") });

            routes.MapRoute("dna_webfiles_paths", "{service}/{website}/{*path}",
                new { website = "home", controller = "WebFiles", action = "PathList" },
                new { service = "webshared", httpMethod = new HttpMethodConstraint("PLIST") });

            routes.MapRoute("dna_webfiles_files", "{service}/{website}/{*path}",
    new { website = "home", controller = "WebFiles", action = "FileList" },
    new { service = "webshared", httpMethod = new HttpMethodConstraint("FLIST") });
            #endregion

            routes.MapRoute("dna_mypages", "{sites}/{website}/{layout}.{extension}", new { controller = "DynamicUI", action = "Index", website = "home" },
                new { sites = "sites", extension = "html" });

            routes.MapRoute("dna_mobile", "{sites}/{website}/{layout}.{extension}", new { controller = "DynamicUI", action = "Mobile", website = "home" }, 
                new { sites = "mobi", extension = "html" });
            routes.MapRoute("dna_mysite", "{website}-{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            if (DnaConfig.IsNeedInstallCheck)
                filters.Add(new InstallationCheckerAttribute());

            var gfilters = DependencyResolver.Current.GetServices<IGlobalFilter>();
            foreach (var filter in gfilters)
                filters.Add(filter);
        }

        public static void RegisterTypes()
        {
            _RegisterUnity();
        }

        public static void SetCulture()
        {
            var httpContext = new HttpContextWrapper(HttpContext.Current);
            var culture = DNA.Mvc.UI.GE.GetCurrentCulture(httpContext);
        }

        //private static void _RegisterComposition()
        //{
        //    var catalog = new DirectoryCatalog(Assembly.GetExecutingAssembly().Location);
        //    var container = new CompositionContainer(catalog);
        //    var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
        //    FilterProviders.Providers.Remove(oldProvider);
        //    var provider = new CompositionFilterAttributeFilterProvider(container);
        //    FilterProviders.Providers.Add(provider);
        //    DependencyResolver.SetResolver(new CompositionDependencyResolver(container));
        //}

        private static void _RegisterUnity()
        {
            var container = new UnityContainer();

            UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            //UnityConfigurationSection configuration = (UnityConfigurationSection)WebConfigurationManager.OpenWebConfiguration("/unity").GetSection("unity");
            configuration.Configure(container);

            //Enabling Dependency Injection for filters
            var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
            FilterProviders.Providers.Remove(oldProvider);
            var provider = new UnityFilterAttributeFilterProvider(container);
            FilterProviders.Providers.Add(provider);

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }

        public static void Init()
        {
            var initializers = DependencyResolver.Current.GetServices<IAppInitializer>();
            if (initializers != null)
            {
                bool isInstalled = !DnaConfig.IsNeedInstallCheck;
                foreach (var initializer in initializers)
                {
                    if (!isInstalled)
                        if (initializer.Timing == InitializeTiming.AfterInstalled) continue;
                        else
                            if (initializer.Timing == InitializeTiming.BeforeInstall) continue;
                    initializer.Init();
                }
            }
        }
    }
}